"""
教师视图层
"""
from lib import common
from interface import common_interface
from interface import teather_interface
#登陆状态
teather_dic = {
    "user":None
}
#登录
#登陆功能
def login():
    while True:
        name = input("请输入用户账号:")
        password = input("请输入用户密码:")
        flag,msg = common_interface.login(name,password,type = "teather")
        if flag:
            teather_dic["user"] = name
            print(msg)
            break
        else:
            print(msg)


#查看教授课程
@common.login("teather")
def select_crouse():
    flag,msg = teather_interface.check_crouse(teather_dic.get("user"))
    if flag:
        print(msg)
    else:
        print(msg)


#选择教授课程
@common.login("teather")
def chioce_crouse():
    while True:
        #打印学校信息
        flag,msg = common_interface.select_school()
        if not flag:
            print(msg)
            continue 
        school_list = msg
        for index,school_name in enumerate(school_list):
            print(f"编号{index}==校区名字{school_name}")
        chioce = input("选择一个校区")
        if not chioce.isdigit():
            print("请输入正确的编号")
            continue
        chioce = int(chioce)
        if chioce not in range(len(school_list)):
            print("请输入正确的编号")
            continue
        school_name = school_list[chioce]
        #打印课程信息
        flag1,msg1 = common_interface.select_school_crouse(school_name)
        if not flag1:
            print(msg1)
            continue 
        crouse_list = msg1
        for index1,crouse_name in enumerate(crouse_list):
            print(f"编号{index1}==课程名字{crouse_name}")
        chioce1 = input("选择一个课程")
        if not chioce1.isdigit():
            print("请输入正确的编号")
            continue
        chioce1 = int(chioce1)
        if chioce1 not in range(len(crouse_list)):
            print("请输入正确的编号")
            continue
        crouse_name = crouse_list[chioce1]
        flag2,msg2 = teather_interface.add_crouse(crouse_name,teather_dic["user"])  
        if flag2:
            print(msg2)
            break
        else:
            print(msg2)   
    

#查看课程下学生
@common.login("teather")
def select_student():
    while True:
        flag,msg = teather_interface.check_crouse(teather_dic.get("user"))
        if not flag:
            print(msg)
            continue 
        crouse_list = msg
        for index1,crouse_name in enumerate(crouse_list):
            print(f"编号{index1}==课程名字{crouse_name}")
        chioce1 = input("选择一个课程")
        if not chioce1.isdigit():
            print("请输入正确的编号")
            continue
        chioce1 = int(chioce1)
        if chioce1 not in range(len(crouse_list)):
            print("请输入正确的编号")
            continue
        crouse_name = crouse_list[chioce1]
        flag2,msg2 = teather_interface.cheak_student(crouse_name,teather_dic["user"])
        if flag2:
            print(msg2)
            break
        else:
            print(msg2)         

        
    
#修改学生分数
@common.login("teather")
def select_sroce():
    while True:
        #先获取课程
        #打印学校信息
        flag,msg = common_interface.select_school()
        if not flag:
            print(msg)
            continue 
        school_list = msg
        #学校
        for index,school_name in enumerate(school_list):
            print(f"编号{index}==校区名字{school_name}")
        chioce = input("选择一个校区")
        if not chioce.isdigit():
            print("请输入正确的编号")
            continue
        chioce = int(chioce)
        if chioce not in range(len(school_list)):
            print("请输入正确的编号")
            continue
        school_name = school_list[chioce]
        flag1,msg1 = common_interface.select_school_crouse(school_name)
        if not flag1:
            print(msg1)
            continue 
        crouse_list = msg1
        #课程
        for index1,crouse_name in enumerate(crouse_list):
            print(f"编号{index1}==课程名字{crouse_name}")
        chioce1 = input("选择一个课程")
        if not chioce1.isdigit():
            print("请输入正确的编号")
            continue
        chioce1 = int(chioce1)
        if chioce1 not in range(len(crouse_list)):
            print("请输入正确的编号")
            continue
        crouse_name = crouse_list[chioce1]
        flag2,msg2 = teather_interface.cheak_student(crouse_name,teather_dic["user"])
        if not flag2:
            print(msg2)
            continue
        student_list = msg2
        #学生
        for index2,student_name in enumerate(student_list):
            print(f"编号{index2}==课程名字{student_name}")
        chioce2 = input("选择一个课程")
        if not chioce2.isdigit():
            print("请输入正确的编号")
            continue
        chioce2 = int(chioce2)
        if chioce2 not in range(len(student_list)):
            print("请输入正确的编号")
            continue
        student_name = student_list[chioce2]
        sorce = input("请输入修改的分数")
        flag4,msg4 = teather_interface.change_sorce(teather_dic["user"],student_name,sorce,crouse_name)
        if flag4:
            print(msg4)
            break



func_dic={
    "1":login,
    "2":select_crouse,
    "3":chioce_crouse,
    "4":select_student,
    "5":select_sroce,
}

def teather_view():
    while True:
        print("""
        =================
            1.登录
            2.查看教授课程
            3.选择教授课程
            4.查看课程下学生
            5.修改学生分数
        =================
        """)
        choice = input("请按编号选择一个功能(输入q退出此功能):")
        if choice == "q":
            break
        if choice  not in func_dic:
            print("请输入正确得编号")
            continue
        func_dic.get(choice)()       